<?php
namespace app\admin\controller;

use app\model\Enter as EnterModel;

class Enter extends Auth
{
    /**
     * 后台审核列表
     *
     * @return void
     */
    public function list()
    {
        $where = [];
        // 申请身份 1城市合伙人 2县级合伙人 3乡镇合伙人
        $shenfen = input('shenfen');
        if (!empty($shenfen)) {
            $where[] = ['shenfen', '=', $shenfen];
        }

        // 审核人
        $boss_id = input('boss_id');
        if (!empty($boss_id)) {
            $where[] = ['boss_id', '=', $boss_id];
        } elseif ($boss_id === 0) {
            $where[] = ['boss_id', '=', null];
        }

        // 创建时间
        $start_time = input('start_time');
        if (!empty($start_time)) {
            $end_time = date('Y-m-d', (strtotime($start_time) + 86399));
            $where[] = ['create_time', 'between', "{$start_time}, {$end_time}"];
        }

        $page = input('page');
        if (empty($page)) {
            $page = 1;
        }
        $limit = input('limit');
        if (empty($limit)) {
            $limit = 10;
        }
        $page_count = ($page - 1) * $limit;

        $list['data'] = EnterModel::field('id, shenfen, name, enter_sheng, enter_shi, enter_xian, enter_xiang, enter_address_details, boss_id, phone, create_time, shenhe_time, type, cause')
            ->where($where)
            ->limit($page_count, $limit)
            ->select()
            ->toArray();
        foreach ($list['data'] as $k => $v) {
            if ($v['boss_id'] == null) {
                $list['data'][$k]['boss_name'] = '管理员';
                $list['data'][$k]['boss_shenfen'] = '';
            } else {
                $boss_info = EnterModel::where('user_id', $v['boss_id'])->find();
                $list['data'][$k]['boss_name'] = $boss_info['name'];
                $list['data'][$k]['boss_shenfen'] = $boss_info['shenfen'];
            }
        }
        $list['count'] = EnterModel::where($where)->count();
        $list['page'] = $page;
        success('入驻审核列表', $list);
    }

    /**
     * 通过、驳回入驻申请
     *
     * @return void
     */
    public function setType()
    {
        $data = [
            'id'    => input('id'),
            'type'  => input('type'),
            'cause' => input('cause'),
            'num'   => input('num')
        ];

        if (empty($data['id']) || empty($data['type']) || empty($data['num'])) {
            error('缺少参数');
        }

        if ($data['type'] == 4 && empty($data['cause'])) {
            error('驳回原因不能为空');
        }
        if (EnterModel::where('num', $data['num'])->find()) {
            error('编号重复');
        }

        $type = EnterModel::where('id', $data['id'])->value('type');
        if ($type > 1) {
            error('请等待下级审核先通过');
        }

        try {
            EnterModel::where('id', $data['id'])->update(['type' => 0, 'cause' => $data['cause'], 'num' => $data['num'], 'shenhe_time' => date('Y-m-d H:i:s', time())]);
        } catch (\Exception $e) {
            error($e->getMessage());
        }
        success('操作成功');
    }

    /**
     * 查看详情
     *
     * @return void
     */
    public function view()
    {
        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }

        $info = EnterModel::field('id, shenfen, enter_sheng, enter_shi, enter_xian, enter_xiang, enter_address_details, boss_id, name, sex, birth_time, id_num, phone, census_address, census_address_details, create_time')
            ->where('id', $id)
            ->withAttr('census_address', function($value, $data) {
                return json_decode($value);
            })
            ->find();
        if ($info['boss_id'] == null) {
            $info['boss_name'] = '管理员';
            $info['boss_shenfen'] = '';
        } else {
            $boss_info = EnterModel::where('user_id', $info['boss_id'])->find();
            $info['boss_name'] = $boss_info['name'];
            $info['boss_shenfen'] = $boss_info['shenfen'];
        }

        success('查看详情', $info);
    }

    /**
     * 审核人列表
     *
     * @return void
     */
    public function bossList()
    {
        $list = EnterModel::field('user_id, name')->where('shenfen', 'in', '1, 2')->where('type', 0)->order('shenfen asc')->select()->toArray();
        array_unshift($list, ['user_id' => 0, 'name' => '管理员']);
        success('审核人列表', $list);
    }
}